/**
 * FilterField.java 2008-05-14
 * 
 * Linker is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package linker.gui;

import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import linker.list.Listmodel;

/**
 * The filterfield above the view list.When the text in it changed, it will act
 * to resort the viewlist's elments.It is used to find something in a huge list
 * easily.
 * 
 * @version 2008-05-14
 * @author Jianfeng tujf.cn@gmail.com
 * 
 */

class FilterField extends JTextField implements DocumentListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The relatived JList.
	 */
	private JList list;

	/**
	 * 
	 * @param width
	 *            The JTextField's width.
	 * @param searchList
	 *            The relatived list.
	 */
	public FilterField(final int width, final JList searchList) {
		super(width);

		getDocument().addDocumentListener(this);

		this.list = searchList;
	}

	/**
	 * @param e
	 *            DocumentEvent: The text in the textField
	 */
	public void changedUpdate(final DocumentEvent e) {
		((Listmodel) list.getModel()).refilter(getText());
	}

	/**
	 * @param e
	 *            DocumentEvent: Some text inserted to the textField.
	 */
	public void insertUpdate(final DocumentEvent e) {
		((Listmodel) list.getModel()).refilter(getText());
	}

	/**
	 * @param e
	 *            DocumentEvent: Some text removed in the textField.
	 */
	public void removeUpdate(final DocumentEvent e) {
		((Listmodel) list.getModel()).refilter(getText());
	}
}
